<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-item label="个体户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gthxm']" type="text" placeholder="请输入个体户姓名" disabled="disabled"></a-input>
              <a-input v-decorator="['gshmc']" type="hidden"></a-input>
              <a-input v-decorator="['ids']" type="hidden"></a-input>
              <a-input v-decorator="['sh']"  type="hidden"></a-input>
              <a-input v-decorator="['gsmc']" type="hidden"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="纳税人识别号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['nsrsbh']" placeholder="请输入纳税人识别号" disabled="disabled"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="服务名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fwmc']" placeholder="请输入货物或应税劳务、服务名称" disabled="disabled"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['kpje']" placeholder="请输入开票金额" disabled="disabled"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="当前是否超定额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['sfcde']" :trigger-change="true" dictCode="lgdk_sfcde" placeholder="请选择当前是否超定额" disabled="disabled"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="当前待开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dkpje']" placeholder="请输入当前待开票金额" disabled="disabled"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="不限制" v-decorator="['startTime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled="disabled"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="不限制" v-decorator="['endTime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled="disabled"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <a-modal
      title="查看验收单"
      :visible="visibleYsd"
      width="1200px"
      cancelText="关闭"
      @cancel="HzClose"
    >
      <div  id="hz" style="width:1160px;height:475px;overflow-y:auto;">

        <iframe style="width: 100%;height: 100%;" :src="ysdImgUrl"></iframe>
      </div>

      <template slot="footer">
        <a-button @click="HzClose">关闭</a-button>
      </template>

    </a-modal>
      <!-- 子表单区域 -->
    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
      <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ rowSum }}</a>项
      <i style="margin-left: 26px;"></i> 累计开票金额为 <a style="font-weight: 600">{{ kpjeSum }}</a>元
      <i style="margin-left: 26px;"></i> 请勾选并点击确定开具发票
      <!--<a style="margin-left: 24px" @click="onClearSelectedNow">清空</a>-->
    </div>
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="灵工代开发票明细" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="lhygLgdkInvoiceDetailTable.loading"
          :columns="lhygLgdkInvoiceDetailTable.columns"
          :dataSource="lhygLgdkInvoiceDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :actionButton="false"
          @selectRowChange="handleSelectRowChange"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          @change="handleTableChange">
          <template v-slot:action="props">
            <a @click="pdf(props)">预览</a>
          </template>
        </j-editable-table>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditableTable from '@/components/jeecg/JEditableTable'

  export default {
    name: 'LhygLgdkInvoiceForm',
    mixins: [JEditableTableMixin],
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
      JEditableTable,
    },
    data() {
      return {
        visibleYsd:false,
        ysdImgUrl:'',
        kpjeSum: 0,
        rowSum: 0,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['lhygLgdkInvoiceDetail', ],
        tableKeys:['lhygLgdkInvoiceDetail', ],
        activeKey: 'lhygLgdkInvoiceDetail',
        // 灵工代开发票明细
        lhygLgdkInvoiceDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '项目单号',
              key: 'xmdh',
              type: FormTypes.text,
              width:"200px",
              align:"center",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '任务金额',
              key: 'rwje',
              type: FormTypes.text,
              width:"80px",
              align:"right",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '银行收款',
              key: 'yhsk',
              type: FormTypes.text,
              width:"80px",
              align:"right",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '个体户到账时间',
              key: 'dzsj',
              type: FormTypes.text,
              width:"150px",
              align:"center",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '银行流水号',
              key: 'yhls',
              type: FormTypes.text,
              width:"200px",
              align:"center",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '合同编号',
              key: 'htbh',
              type: FormTypes.text,
              width:"200px",
              align:"center",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '验收单号',
              key: 'ysdh',
              type: FormTypes.text,
              width:"200px",
              align:"center",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '验收单',
              key: 'ysdpdf',
              width: '6%',
              type: FormTypes.slot, // 定义该列为 自定义插值列
              defaultValue: 'ysdpdf',
              slotName: 'action' // slot 的名称，对应 v-slot 冒号后面和等号前面的内容
            }
          ]
        },
        url: {
          add: "/lgdkinvoice/lhygLgdkInvoice/add",
          edit: "/lgdkinvoice/lhygLgdkInvoice/edit",
          queryById: "/lgdkinvoice/lhygLgdkInvoice/queryById",
          lhygLgdkInvoiceDetail: {
            list: '/lgdkinvoice/lhygLgdkInvoice/queryLhygLgdkInvoiceDetailByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      pdf(props) {
        let that = this;
        that.ysdImgUrl = props.text;
        that.visibleYsd = true;
      },
      HzClose(){
        this.visibleYsd = false;
      },
      addBefore(){
        this.form.resetFields()
        this.lhygLgdkInvoiceDetailTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'gthxm','nsrsbh','fwmc','kpje','sfcde','dkpje','startTime','endTime','gshmc','ids','sh','gsmc')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.nsrsbh) {
          var tStartTime = this.model.startTime;
          var tEndTime = this.model.endTime;
          if(undefined===tStartTime){
            tStartTime = '';
          }
          if(undefined===tEndTime){
            tEndTime = '';
          }
          let params = { nsrsbh: this.model.nsrsbh, startTime: tStartTime, endTime: tEndTime, sh: this.model.sh, gsmc: this.model.gsmc}
          this.requestSubTableData(this.url.lhygLgdkInvoiceDetail.list, params, this.lhygLgdkInvoiceDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          lhygLgdkInvoiceDetailList: allValues.tablesValue[0].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'gthxm','nsrsbh','fwmc','kpje','sfcde','dkpje','startTime','endTime','gshmc','ids','sh','gsmc'))
     },

      onClearSelectedNow() {
        this.kpjeSum = 0;
        this.rowSum = 0;
      },
      handleSelectRowChange(event){
        let that = this;
        let m = event;
        this.rowSum = m.length;
        let a = that.lhygLgdkInvoiceDetailTable.dataSource;
        var kpjeSumTemp = 0
        var idsTemp = ''
        a.forEach((item) => {
          if(m.indexOf(item.id)>-1){
            kpjeSumTemp = (kpjeSumTemp-0) + (item.rwje-0);
            idsTemp = idsTemp + "'" + item.id + "',";
          }
        })
        if(idsTemp.length>0){
          idsTemp = idsTemp.substring(0,idsTemp.length-1);
        }
        this.kpjeSum = kpjeSumTemp;
        this.form.setFieldsValue({ids:idsTemp});
        this.form.setFieldsValue({kpje:this.kpjeSum});
        if(this.kpjeSum>=40000){
          this.form.setFieldsValue({sfcde:'是'});
        }else{
          this.form.setFieldsValue({sfcde:'否'});
        }
      },
    }
  }
</script>

<style scoped>
</style>